package model


const collectionName string = "blog"

type Blog struct {
	Id int `json:"id" bson:"_id"`
	Title string `json:"title" bson:"title"`
	Content string `json:"content" bson:"content"`
	Uid int `json:"uid" bson:"uid"`
}

func (self *Blog) Save() (int,error) {
	session := mongoSession.Copy()
	defer session.Close()

	mc := session.DB(DB).C(collectionName)
	err := mc.Insert(self)
	if err != nil {
		return -1,err
	}
	return 1,nil
}

func (self *Blog) Get() (*Blog,error) {
	session := mongoSession.Copy()
	defer session.Close()

	mc := session.DB(DB).C(collectionName)

	var blog Blog
	param := make(map[string]interface{})
	param["_id"] = self.Id
	err := mc.Find(param).One(&blog)
	return &blog,err
}